package com.chainyoung.component_tmtrade.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.view.CircleImageView;
import com.chainyoung.component_tmtrade.R;
import com.chainyoung.component_tmtrade.bean.FiatTradeListItem;
import com.chainyoung.component_tmtrade.ui.activity.SubmitFiatCurrencyTradeOrderActivity;
import com.chainyoung.component_tmtrade.ui.view.RecieveAccountsView;

import java.util.ArrayList;
import java.util.List;

import static com.chainyoung.common.constant.CommonStringConstants.NUM;
import static com.chainyoung.common.constant.CommonStringConstants.TYPE;
import static com.chainyoung.component_tmtrade.constant.StringConstant.FIAT_COIN_NAME;
import static com.chainyoung.component_tmtrade.constant.StringConstant.FIAT_CURRENCY_TRADE_ITEM;

public class TradeFiatCurrencyBuyAdapter extends BaseQuickAdapter<FiatTradeListItem, BaseViewHolder> {

    private int type;//1:买 2：卖
    private String coinName;//法币名称


    public TradeFiatCurrencyBuyAdapter(@Nullable List<FiatTradeListItem> data, int type, String coinName) {
        super(R.layout.tmtrade_list_fiat_currency_trade_item, data);
        this.type = type;
        this.coinName = coinName;
    }

    public void setCoinName(String coinName) {
        this.coinName = coinName;
    }


    @Override
    protected void convert(BaseViewHolder helper, final FiatTradeListItem item) {

        helper.setText(R.id.tv_price, "¥"+item.getPrice_cny());
        helper.setText(R.id.tv_num, "数量 " + item.getNum());
        helper.setText(R.id.tv_limit_amount, "限额(CNY) " + item.getTrade_min() + "～" + item.getTrade_max());
        helper.setBackgroundRes(R.id.tv_buy, type == 1 ? R.drawable.tmtrade_shape_green_1 : R.drawable.tmtrade_shape_red_1);
        helper.setText(R.id.tv_buy, type == 1 ? R.string.buy : R.string.sell_out);

        CircleImageView ivLogo = helper.getView(R.id.iv_logo);
        Glide.with(mContext).load(item.getAvatar()).into(ivLogo);

        helper.setText(R.id.tv_name, item.getUsername());
        helper.setText(R.id.tv_month_num, "月单数：" + item.getTrade_num());

        RecieveAccountsView llAccounts = helper.getView(R.id.ll_accounts);
        if (item.getTrade_type() == null || item.getTrade_type().isEmpty()) {
            return;
        } else {
            List<Integer> types = new ArrayList<>();
            for (FiatTradeListItem.TradeTypeBean itemBean : item.getTrade_type()) {
                types.add(itemBean.getType());
            }
            llAccounts.setViews(types);
        }


        helper.getView(R.id.rl_buy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SubmitFiatCurrencyTradeOrderActivity.class);
                intent.putExtra(TYPE, type);
                intent.putExtra(FIAT_CURRENCY_TRADE_ITEM, item);
                intent.putExtra(NUM, item.getNum());
                intent.putExtra(FIAT_COIN_NAME, coinName);
                mContext.startActivity(intent);
            }
        });
    }
}
